import { login } from '@/api/login.js'
import { makeObservable, observable, action } from 'mobx'

class Login {
  constructor() {
    makeObservable(this, {
      token: observable,
      login: action.bound,
      delToken: action.bound
    })
  }
  token = localStorage.getItem('react-token') || ''

  async login(data) {
    const {
      data: { token }
    } = await login(data)
    this.token = token
    localStorage.setItem('react-token', token) // 数据持久化
  }

  delToken() {
    this.token = ''
    localStorage.removeItem('react-token')
  }
}

export default Login
